/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jaspec;

import static junit.framework.Assert.*;

/**
 * Jaspec number handler.
 */
public class JaspecNumber extends JaspecObject<Number> {

  public JaspecNumber(Number obj) {
    super(obj);
  }

  public JaspecNumber(Number obj, boolean not) {
    super(obj, not);
  }

  /**
   * Check that the number is within a range.
   * 
   * @param min Min number
   * @param max Max number
   */
  public void be_within(Number min, Number max) {
    assertTrue("The given range is invalid.", min.doubleValue() <= max.doubleValue());
    boolean within =
        obj.doubleValue() >= min.doubleValue() && obj.doubleValue() <= max.doubleValue();
    if (!not) {
      assertTrue("The number " + obj + " should be within " + min + " and " + max, within);
    } else {
      assertFalse("The number " + obj + " should not be within " + min + " and " + max, within);
    }
  }

  /**
   * Check that the number is bigger than other number.
   * 
   * @param n The other number
   */
  public void bigger_than(Number n) {
    boolean bigger = obj.doubleValue() > n.doubleValue();
    if (!not) {
      assertTrue("The number " + obj + " should be bigger than " + n, bigger);
    } else {
      assertFalse("The number " + obj + " should not be bigger than " + n, bigger);
    }
  }

  /**
   * Check that the number is smaller than other number.
   * 
   * @param n The other number
   */
  public void smaller_than(Number n) {
    boolean smaller = obj.doubleValue() < n.doubleValue();
    if (!not) {
      assertTrue("The number " + obj + " should be smaller than " + n, smaller);
    } else {
      assertFalse("The number " + obj + " should not be smaller than " + n, smaller);
    }
  }

  /**
   * Check that the number is bigger or equal than other number.
   * 
   * @param n The other number
   */
  public void bigger_or_equal_than(Number n) {
    boolean biggerOrEqual = obj.doubleValue() >= n.doubleValue();
    if (!not) {
      assertTrue("The number " + obj + " should be bigger or equal than " + n, biggerOrEqual);
    } else {
      assertFalse("The number " + obj + " should not be bigger or equal than " + n, biggerOrEqual);
    }
  }

  /**
   * Check that the number is smaller or equal than other number.
   * 
   * @param n The other number
   */
  public void smaller_or_equal_than(Number n) {
    boolean smallerOrEqual = obj.doubleValue() <= n.doubleValue();
    if (!not) {
      assertTrue("The number " + obj + " should be smaller or equal than " + n, smallerOrEqual);
    } else {
      assertFalse("The number " + obj + " should not be smaller or equal than " + n, smallerOrEqual);
    }
  }

  /**
   * Check that the number is 1.
   */
  public void be_1() {
    boolean is1 = obj.doubleValue() == 1;
    if (!not) {
      assertTrue("The number " + obj + " should be 1", is1);
    } else {
      assertFalse("The number " + obj + " should not be 1", is1);
    }
  }

  /**
   * Check that the number is 2.
   */
  public void be_2() {
    boolean is2 = obj.doubleValue() == 2;
    if (!not) {
      assertTrue("The number " + obj + " should be 2", is2);
    } else {
      assertFalse("The number " + obj + " should not be 2", is2);
    }
  }

  /**
   * Check that the number is multiple of other number.
   * 
   * @param n The other number
   */
  public void be_multiple_of(Number n) {
    boolean multipleOf = obj.doubleValue() % n.doubleValue() == 0;
    if (!not) {
      assertTrue("The number " + obj + " should multiple of " + n, multipleOf);
    } else {
      assertFalse("The number " + obj + " should not be multiple of " + n, multipleOf);
    }
  }

  /**
   * Check that the number is pair.
   */
  public void be_pair() {
    be_multiple_of(2);
  }
}
